# ** por compatibilidad se omiten tildes **
# ==============================================================================
# TRABAJO PRACTICO 3 - System Programming - ORGANIZACION DE COMPUTADOR II - FCEN
# ==============================================================================

MCOPY=mcopy

KERNEL_SRC=kernel.asm
KERNEL_OBJ=kernel.o
KERNEL_BIN=kernel.bin

DEPEND = Makefile \
	kernel.asm isr.asm \
	imprimir.mac \
	gdt.c gdt.h pic.c pic.h idt.c idt.h isr.h tss.c tss.h mmu.c mmu.h \
	sched.c sched.h screen.c screen.h \
	game.c game.h syscall.h

OBJ=$(KERNEL_OBJ) gdt.o pic.o idt.o isr.o mmu.o sched.o tss.o screen.o game.o
DISK_IMG=diskette.img
DISK_IMG_BZ=diskette.img.bz2

CFLAGS=-m32 -g -ggdb -Wall -Werror -O0 \
  -fno-zero-initialized-in-bss -fno-stack-protector -ffreestanding


TASK0=idle.tsk
TASK1=task1.ctsk
TASK2=task2.ctsk
TASK3=task3.ctsk
TASK4=task4.ctsk
TASK5=task5.ctsk

TASKSBINS=$(TASK0) $(TASK1) $(TASK2) $(TASK3) $(TASK4) $(TASK5)

TASKS=tareas.tsk
KERNELSIZE=85504
CODESIZE=60928
TASKSSIZE= 28672 # = 24576+4096

CC=gcc
NASM=nasm
NASMFLAGS=-felf32
LD=ld
LDTASKFLAGS=-s -static -m elf_i386 --oformat binary -b elf32-i386 -e task -Ttext 0x3A0000
LDFLAGS=-static -m elf_i386 --oformat binary -b elf32-i386 -e start -Ttext 0x1200

QUIET = @

.PHONY=clean all image tasks

all: cleantasks tasks image #clean

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $^

%.o: %.asm
	$(NASM) $(NASMFLAGS) -o $@ $^

kernel.bin: $(OBJ) $(TASKS)
	@echo 'Linkeando kernel...'
	$(LD) $(LDFLAGS) -o $@ $(OBJ)
	@echo ''

	@echo 'Generando imagen del kernel...'
	mv kernel.bin kernel.bin.tmp
	dd if=/dev/zero of=kernel.bin bs=1 count=$(KERNELSIZE) conv=notrunc status=noxfer > /dev/null 2>&1
	dd if=kernel.bin.tmp of=kernel.bin bs=1 count=$(CODESIZE) conv=notrunc status=noxfer > /dev/null 2>&1
	dd if=$(TASKS) of=kernel.bin bs=1 count=$(TASKSSIZE) seek=$(CODESIZE) conv=notrunc status=noxfer > /dev/null 2>&1
	@echo ''

image: kernel.bin $(DEPEND) $(DISK_IMG)
	@echo 'Copiando el $(KERNEL_BIN) a la imagen de diskette'
	$(MCOPY) -o -i $(DISK_IMG) $(KERNEL_BIN) ::/
	@echo ''

$(DISK_IMG): $(DISK_IMG_BZ)
	bzip2 -dk $(DISK_IMG_BZ)

%.tsk: %.asm
	@echo 'Compilando tarea...'
	$(NASM) -fbin -o $@ $^
	@echo ''

task1.tsko: tarea1.c
	@echo 'Compilando tarea...'
	$(CC) $(CFLAGS) -Dtask1 -c -o $@ $^
	@echo ''

task5.tsko: arbitro.c
	@echo 'Compilando tarea...'
	$(CC) $(CFLAGS) -Dtask5 -c -o $@ $^
	@echo ''

task5.ctsk: task5.tsko
	@echo 'Linkeando tarea...'
	$(LD) $(LDTASKFLAGS) -o $@.tmp $^
	@echo ''

	@echo 'Generando imagen de la tarea...'
	dd if=/dev/zero bs=1 count=4K of=$@ status=noxfer > /dev/null 2>&1
	dd if=$@.tmp bs=1 count=4K seek=0 conv=notrunc of=$@ status=noxfer > /dev/null 2>&1
	rm $@.tmp
	@echo ''

%.ctsk: %.tsko
	@echo 'Linkeando tarea...'
	$(LD) $(LDTASKFLAGS) -o $@.tmp $^
	@echo ''

	@echo 'Generando imagen de la tarea...'
	dd if=/dev/zero bs=1 count=4096 of=$@ status=noxfer > /dev/null 2>&1
	dd if=$@.tmp bs=1 count=4096 seek=0 conv=notrunc of=$@ status=noxfer > /dev/null 2>&1
	rm $@.tmp
	@echo ''

tasks: $(TASKSBINS)
	@echo 'Generando imagen de las tareas...'
	$(QUIET) dd if=/dev/zero bs=1 count=$(TASKSSIZE) of=$(TASKS) status=noxfer > /dev/null 2>&1

	$(QUIET) dd if=$(TASK0) bs=1 count=4K of=$(TASKS) seek=0   status=noxfer > /dev/null 2>&1
	$(QUIET) dd if=$(TASK1) bs=1 count=4K of=$(TASKS) seek=4K  status=noxfer > /dev/null 2>&1
	$(QUIET) dd if=$(TASK2) bs=1 count=4K of=$(TASKS) seek=8K  status=noxfer > /dev/null 2>&1
	$(QUIET) dd if=$(TASK3) bs=1 count=4K of=$(TASKS) seek=12K status=noxfer > /dev/null 2>&1
	$(QUIET) dd if=$(TASK4) bs=1 count=4K of=$(TASKS) seek=16K status=noxfer > /dev/null 2>&1
	$(QUIET) dd if=$(TASK5) bs=1 count=4K of=$(TASKS) seek=20K status=noxfer > /dev/null 2>&1
	@echo ''

clean:
	@echo 'Limpiando todo...'
	rm -f *.o
	rm -f *.bin
	rm -f *.tmp
	rm -f *.ctsko
	rm -f task1.ctsk
	rm -f *.tsko
	rm -f *.tsk
	rm -f $(DISK_IMG)
	@echo ''

cleantasks:
	@echo 'Limpiando tareas...'
	rm -f *.tsk
	rm -f *.tsko
	@echo ''
